package com.supermap.guangzhou.weifang.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.supermap.guangzhou.weifang.R;


/**
 * Created by XueQing Wang on 2018/5/10.
 */

public class CustomAlertDialog extends Dialog implements View.OnClickListener{

    private TextView tv_title;
    private TextView tv_message;
    private TextView tv_cancel;
    private TextView tv_confirm;
    private String titleContent;    //从外界设置的title文本
    private String messageContent;  //从外界设置的message文本
    private String yesContent;      //确定文本内容
    private String noContent;       //取消文本内容
    private boolean isHideCancel;   //是否隐藏取消按钮
    private boolean isCanceledOnTouchOutside;     //是否能够点击外部取消dialog

    private AlertDialogOnClickListener confirmOnClickListener;
    private AlertDialogOnClickListener cancelOnClickListener;

    public interface AlertDialogOnClickListener {
        void onDialogClick(CustomAlertDialog customAlertDialog);
    }

    public
    CustomAlertDialog(Context context) {
        super(context, R.style.MyDialog);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mc_alert_dialog1);
        setCanceledOnTouchOutside(false);

        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();
    }

    private void initView() {
        tv_title = findViewById(R.id.title);
        tv_message = findViewById(R.id.alert_message);

        tv_cancel = findViewById(R.id.btn_cancel);
        if (isHideCancel) {
            tv_cancel.setVisibility(View.GONE);
        }
        tv_confirm = findViewById(R.id.btn_confirm);
    }

    private void initData() {
        //设置title和message
        if (titleContent != null) {
            tv_title.setText(titleContent);
        }

        if (tv_message != null) {
            tv_message.setText(messageContent);
        }

        //设置确定和取消按钮的显示
        if (yesContent != null) {
            tv_confirm.setText(yesContent);
        }

        if (noContent != null) {
            tv_cancel.setText(noContent);
        }
    }

    private void initEvent() {
        tv_confirm.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);
    }

    public void setTitle(String title) {
        this.titleContent = title;
    }

    public void setMessage(String message) {
        this.messageContent = message;
    }

    public void setYesContent(String yesContent) {
        this.yesContent = yesContent;
    }

    public void setNoContent(String noContent) {
        this.noContent = noContent;
    }

    public void hideCancel() {
        isHideCancel = true;
    }

    public void setIsCanceledOnTouchOutside(boolean isCanceledOnTouchOutside) {
        this.isCanceledOnTouchOutside = isCanceledOnTouchOutside;
    }

    public void setConfirmOnClickListener(AlertDialogOnClickListener confirmOnClickListener) {
        this.confirmOnClickListener = confirmOnClickListener;
    }

    public void setCancelOnClickListener(AlertDialogOnClickListener cancelOnClickListener) {
        this.cancelOnClickListener = cancelOnClickListener;
    }

    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        if (viewId == R.id.btn_cancel){
            if (confirmOnClickListener != null) {
                cancelOnClickListener.onDialogClick(this);
            }
        } else if (viewId == R.id.btn_confirm) {
            if (confirmOnClickListener != null) {
                confirmOnClickListener.onDialogClick(this);
            }
        }
    }
}
